#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
from util.app_global import version


__required_dirs = [
    'widget',
    'util',
    'odbc_p',
    'cloghandler',
    'aliyun'
]


__required_files = [
    'conf/logging.conf',
    'OpenDSClient.py',
    'requirement.txt',
    'run.pyw',
    'UI.py',
    'README.md'
]

if len(sys.argv) < 2:
    __version = version
else:
    __version = sys.argv[1]
__dist = 'dist/'
__package_name = 'bdp_sync-%s.unix' % __version
__dst = __dist + __package_name

if not os.path.exists(__dist):
    os.mkdir(__dist)

if os.path.exists(__dst):
    os.system('rm -rf %s' % __dst)

os.mkdir(__dst)


def cp_dir(_dir):
    src = os.path.abspath(_dir)
    dst = os.path.abspath(os.path.join(__dst, _dir))
    print 'Copy dir %s -> %s' % (src, dst)
    os.system('cp -r %s %s' % (src, dst))


def cp_file(_file):
    src = os.path.abspath(_file)
    dst = os.path.abspath(os.path.join(__dst, _file))
    dir_name = os.path.dirname(dst)
    if not os.path.exists(dir_name):
        os.mkdir(dir_name)
    print 'Copy file %s -> %s' % (src, dst)
    os.system('cp %s %s' % (src, dst))


def make_tar():
    cmd = 'tar zcvf dist/%s.tar.gz dist/%s' % (__package_name, __package_name)
    print cmd
    os.system(cmd)


if os.name == 'posix':
    map(cp_dir, __required_dirs)
    map(cp_file, __required_files)
    make_tar()
